<?php

namespace App\Console\Commands\Controller;

use App\Common\Libs\Ali\Oss;
use App\Common\Libs\ArrayHelper;
use App\Common\Libs\Json;
use App\Console\BaseCommand;
use App\Consts\GlobalConst;
use App\Consts\GlobalUploadConst;
use App\Models\Pro;
use App\Models\UserRecommend;

class UserCommand extends BaseCommand
{
    protected $signature = 'controller:user {action} {json}';

    protected $description = 'user commands';

    /**
     * 修改用户地址、学历、职业、工作
     * @param $loadData
     * @return bool
     */
    public function editAddress($loadData)
    {
        $user = repository()->UserRepository->getUserByUserId($loadData['id']);
        if (!$user) {
            return false;
        }

        $areaJson = Oss::getInstance('resources/user/')->getObject('area.json');
        $areaArr = Json::encode($areaJson, true);
        $areaArr = ArrayHelper::index($areaArr, 'id');

        if (array_key_exists('country', $loadData) && !empty($loadData['country']) && !empty($areaArr[$loadData['country']])) {
            $address = $areaArr[$loadData['country']]['name'];
        } else {
            // 中国大陆ID
            $address = $areaArr[224581]['name'];
        }

        if (array_key_exists('province', $loadData) && !empty($loadData['province']) && !empty($areaArr[$loadData['province']])) {
            $address .= ',' . $areaArr[$loadData['province']]['name'];
        }
        if (array_key_exists('city', $loadData) && !empty($loadData['city']) && !empty($areaArr[$loadData['city']])) {
            $address .= ',' . $areaArr[$loadData['city']]['name'];
        }

        $loadData['address'] = $address;

        return $user->update($loadData);
    }

    /**
     * 删除用户头像
     * @param $base64LoadData
     * @return bool
     */
    public function delAvatar($base64LoadData)
    {
        $data = $base64LoadData;

        // 删除旧的头像
        if ($data['oldAvatar'] == $data['newAvatar']) {
            return true;
        }

        Oss::getInstance(GlobalUploadConst::USER_AVATAR_PATH)
            ->deleteOssFileByFilename(GlobalUploadConst::USER_AVATAR_PATH, $data['oldAvatar']);

        // 当前用户如果是智囊团，且智囊团头像和用户旧头像一致，则需要替换成新头像
        $pro = Pro::getModel()->where(['user_id' => $data['user_id']])->first();
        if ($pro && $pro->avatar == $data['oldAvatar']) {
            $pro->avatar = $data['newAvatar'];
            $pro->save();
        }

        return true;
    }

    /**
     * 修改头像后，修改推荐文章卡片头像区域
     * @param $data
     * @return bool
     */
    public function repairRecommendAvatar($data)
    {
        $recommends = UserRecommend::getModel($data['user_id'])
            ->where(['user_id' => $data['user_id'], 'is_deleted' => GlobalConst::IS_NOT_DELETE])->get();

        foreach ($recommends as $recommend) {
            $avatarJson = Oss::getInstance(GlobalUploadConst::RECOMMEND_LIST_AVATAR_AREA)
                ->getObject($recommend->target_id . '.json');
            if ($avatarJson) {
                $avatarArr = Json::decode($avatarJson);
                if (in_array($data['old_avatar'], $avatarArr)) {
                    $index = array_search($data['old_avatar'], $avatarArr);
                    $avatarArr[$index] = $data['new_avatar'];
                    Oss::getInstance(GlobalUploadConst::RECOMMEND_LIST_AVATAR_AREA)
                        ->uploadTarget($avatarArr, true, $recommend->target_id . '.json');
                }
            }
        }

        return true;
    }

}
